<?php
/**
 * Created by xuan.
 * Author: xuan
 * Date: 2018/8/3
 * Time: 18:24
 */
namespace app\api\controller\v1;

use app\api\model\Member as MemberModel;
use app\api\model\Recharge;
use think\Request;
use think\Response;
use app\api\controller\BaseController;
use app\api\model\Payment;
use web\util\WebUtil;
use app\api\service\WithdrawService;
use app\api\model\Goodsorder;
use web\util\WechatUtil;
use app\common\model\RewardModel;
use common\util\ArrayUtil;
use app\common\model\RebateModel;
use think\Log;
use web\util\OrderUtil;
use web\util\MemberUtil;
use common\util\Tools;

class Member extends BaseController {

    private $rechargeModel = null;

    function __construct() {
        parent::__construct();
        $this->rechargeModel = new Recharge();
    }

    /**
     * 代理资料
     */
    public function index() {
        $mid = $this->getLoginId();
        $this->needLogin();

        $datas = MemberModel::selectOneMemberByID($mid);
        $datas2 = array();
        $datas2['id'] = $datas['id'];
        $datas2['name'] = $datas['name'];
        $datas2['weixin'] = $datas['weixin'];
        if ($datas['grade'] == 1) {
            $datas2['grade'] = "品正商";
        } else {
            $datas2['grade'] = "品正商v";
        }
        $datas2['tel'] = $datas['tel'];
        if ($datas['fid'] == 0) {
            $datas2['fname'] = "总部";
        } else {
            $data = MemberModel::selectOneMemberByID($datas['fid']);
            $temp = $data->toArray();
            $datas2['fname'] = $temp['name'];
        }

        $datas2['ifweixin'] = 1; // 是否绑定微信，1绑定，2未绑定
        $datas2['balance'] = $datas['account'] + $datas['rewardaccount'];
        $datas2['avatar'] = get_image($datas['face']); //

        if ($datas) {
            return $this->returnResult(1,'',$datas2);
        } else {
            return $this->returnResult(-2,'');
        }
    }

    /**
     * 用户注册
     */
    public function register() {
        if (request()->isPost()) {
            $all = Request::instance()->param();
            $tel = trim($all['tel']);
            
            if (empty($all['name'])) {
                return $this->returnResult(- 6, '姓名必须填写!');
            }
            if (empty($all['weixin'])) {
                return $this->returnResult(- 6, '微信号必须填写!');
            }
            if (empty($tel)) {
                return $this->returnResult(- 6, '电话必须填写!');
            }
            if (empty($all['addrvalue'])) {
                return $this->returnResult(- 6, '地址必须选择!');
            } elseif (empty($all['idpic2'])) {
                return $this->returnResult(- 6, '支付截图必须上传!');
            }

            // 微信占用
            $dataByWx = MemberModel::instance()->where("weixin", $all['weixin'])
                ->whereIn('status', MemberUtil::getNormalStatus())
                ->find();
            if ($dataByWx) {
                return $this->returnResult(- 3, '该微信号已占用!');
            }

            // 手机占用
            $dataByTel = MemberModel::instance()->where("tel",$tel)
                ->whereIn('status', MemberUtil::getNormalStatus())
                ->find();

            if ($dataByTel) {
                return $this->returnResult(- 4, '该手机号已占用!');
            }
            
            $all['password'] = getPwd($tel);
            $all['tel'] = $tel;
            $all['face'] = '';
            $all['passport_uid'] = 0;
            $all['openid'] = '';
            $all['bind_time'] = 0;
            
            // 获取头像
            $passprtUser = WechatUtil::getPassportUser();
            if ($passprtUser) {
                $passportUid = $passprtUser['id'];
                // 查询用户微信id是否绑定
                $find = MemberModel::instance()->where('passport_uid', $passportUid)
                    ->whereIn('status', MemberUtil::getNormalStatus())
                    ->count();
                if ($find) {
                    return $this->returnResult(- 3, '微信已经绑定了别的账号,请先解绑!');
                }
                $all['face'] = $passprtUser['avatar'];
                $all['passport_uid'] = $passportUid;
                $all['openid'] = $passprtUser['openids'][get_appid()];
                $all['bind_time'] = time();
            }

            $pid_path = '';
            if (isset($all['fid']) && $all['fid']) {
                $pmember = MemberModel::selectOneMemberByID($all['fid']);
                if ($pmember) {
                    if ($pmember['pid_path']) {
                        $pid_path = $pmember['pid_path'];
                        $pid_path .= "," . $all['fid'];
                    } else {
                        $pid_path = $all['fid'];
                    }
                } else {
                    $all['fid'] = 0;
                }
            }
            $all['pid_path'] = $pid_path;

            $data = MemberModel::addOneMember($all);
            if ($data) {
                return $this->returnResult(1,'提交成功,等待审核');
            } else {
                return $this->returnResult(-1,'');
            }
        } else {
            return $this->returnResult(-1,'');
        }
    }

    public function login() {
        if (! request()->isPost()) {
            return $this->returnResult(- 6, '请求错误');
        }

        $all = Request::instance()->param();
        if (empty($all['tel'])) {
            return $this->returnResult(- 6, '手机号必须填写!');
        } elseif (empty($all['password'])) {
            return $this->returnResult(- 6, '密码必须填写!');
        }
        $tel = trim($all['tel']);
        // 通过和未通过可以登录
        $dataByTel = MemberModel::instance()->where("tel", $tel)
            ->whereIn('status', MemberUtil::getNormalStatus())
            ->find();
        ;
        if (empty($dataByTel)) {
            return $this->returnResult(- 3, '账号不存在!');
        }
        $dataByTel = $dataByTel->toArray();
        if ($dataByTel['password'] != md5($all['password'])) {
            return $this->returnResult(- 4, '账号密码错误!');
        }
        if ($dataByTel['status'] == 2) {
            return $this->returnResult(- 5, '账号审核中!');
        } else {
            $this->putLoginInfo($dataByTel['id']);
            return $this->returnResult(1, '登录成功!', $dataByTel);
        }
    }

    /**
     * 退出登录
     */
    public function outlogin() {
        $this->clearLogin();
        return $this->returnResult(1);
    }

    /**
     * 充值
     */
    public function recharge() {
        $mid = $this->getLoginId();
        if (empty($mid)) {
            return $this->returnResult(- 7, '请登录!');
        }
        $money = $this->request->param('money', 0, 'float');
        $pic = $this->request->param("pic", '');
        if ($money <= 0) {
            return $this->returnResult(- 1, '金额错误');
        }
        if(empty($pic)){
            return $this->returnResult(- 1, '图片没有上传');
        }
        $max = 1000000;
        if ($money > $max) {
            return $this->returnResult(- 1, '最大充值金额:' . $max);
        }

        $data = [
            'mid' => $mid,
            'type' => 1,
            'money' => $money,
            'ifpass' => 0,
            'cash_name' => '充值',
            'addtime' => Tools::getCurDate(),
            'pic' => $pic,
            'date' => date('Ymd', time())
        ];
        $id = $this->rechargeModel->insert($data);
        if ($id) {
            return $this->returnResult(1);
        } else {
            return $this->returnResult(0, '充值失败');
        }
    }

    public function recharge_list() {
        $mid = $this->getLoginId();
        if (empty($mid)) {
            return $this->returnResult(- 7, '请登录!');
        }
        $draw = $this->request->param('draw', '1', 'intval');
        $count = $this->rechargeModel->getRechargeListCount($mid);
        $items = $this->rechargeModel->getRechargeList($mid);
        $list = [];
        $memberMap = [];
        foreach ($items as $item) {
            $member = null;
            $smid = $item['mid'];
            if (array_key_exists($smid, $memberMap)) {
                $member = $memberMap[$smid];
            } else {
                $member = MemberModel::selectOneMemberByID($smid);
                $memberMap[$smid] = $member;
            }
            $temp = [
                'id' => $item['id'],
                'nickname' => $member['name'],
                'avatar' => $member['face'],
                'money' => $item['money'],
                'type_name' => '充值',
                'status' => $item['ifpass'],
                'status_name' => $this->_getStatusName($item['ifpass']),
                'ctime' => date('Y/m/d', strtotime($item['addtime']))
            ];
            $list[] = $temp;
        }
        $data = array(
            "returnCode" => 1,
            "returnMsg" => "success!",
            'recordsTotal' => $count,
            'recordsFiltered' => $count,
            'draw' => $draw,
            'data' => $list
        );
        return json($data);
    }

    /**
     * 充值
     */
    public function recharge_detail() {
        $mid = $this->getLoginId();
        if (empty($mid)) {
            return $this->returnResult(- 7, '请登录!');
        }
        $id = $this->request->param('id', '0', 'intval');
        if (empty($id)) {
            return $this->returnResult(- 1, '参数错误');
        }
        $item = $this->rechargeModel->getById($mid, $id);
        if (empty($item)) {
            return $this->returnResult(- 1, '参数错误');
        }

        $member = MemberModel::selectOneMemberByID($mid);
        $data = [
            'id' => $item['id'],
            'nickname' => $member['name'],
            'avatar' => $member['face'],
            'money' => $item['money'],
            'type_name' => '充值',
            'reviewer' => '总部',
            'status' => $item['ifpass'],
            'status_name' => $this->_getStatusName($item['ifpass']),
            'ctime' => date('Y/m/d', strtotime($item['addtime']))
        ];
        return $this->returnResult(1, '', $data);
    }

    /**
     * 提现
     */
    public function withdraw() {
        $mid = $this->getLoginId();
        if (empty($mid)) {
            return $this->returnResult(- 7, '请登录!');
        }
        $money = $this->request->param('money', 0, 'floatval');

        $max = 1000000;
        if ($money > $max) {
            return $this->returnResult(- 1, '最大提现金额:' . $max);
        }
        $all = $this->request->param('all', 0, 'intval');
        $bankId = $this->request->param("bank_id", '0', 'intval');
        if (empty($bankId)) {
            return $this->returnResult(- 1, '请选择提现的账户');
        }
        if (! ($money > 0 || $all)) {
            return $this->returnResult(- 1, '参数错误');
        }
        $result = WithdrawService::withdraw($mid, $money, $all, $bankId);
        list ($res, $msg) = $result;
        if ($res) {
            return $this->returnResult(1);
        } else {
            return $this->returnResult(0, $msg);
        }
    }

    /**
     * 提现列表
     */
    public function withdraw_list() {
        $mid = $this->getLoginId();
        if (empty($mid)) {
            return $this->returnResult(- 7, '请登录!');
        }
        $draw = $this->request->param('draw', '1', 'intval');
        if (empty($draw) || $draw <= 0) {
            $draw = 1;
        }
        $count = $this->rechargeModel->getWithdrawListCount($mid);
        $items = $this->rechargeModel->getWithdrawList($mid, $draw);
        $list = [];
        $memberMap = [];
        foreach ($items as $item) {
            $member = '';
            $smid = $item['mid'];
            if (array_key_exists($smid, $memberMap)) {
                $member = $memberMap[$smid];
            } else {
                $member = MemberModel::selectOneMemberByID($smid);
                $memberMap[$smid] = $member;
            }
            $temp = [
                'id' => $item['id'],
                'nickname' => $member['name'],
                'avatar' => $member['face'],
                'money' => $item['money'],
                'type_name' => '提现',
                'status' => $item['ifpass'],
                'status_name' => $this->_getStatusName($item['ifpass']),
                'ctime' => date('Y/m/d', strtotime($item['addtime']))
            ];
            $list[] = $temp;
        }

        $data = array(
            "returnCode" => 1,
            "returnMsg" => "success!",
            'recordsTotal' => $count,
            'recordsFiltered' => $count,
            'draw' => $draw,
            'data' => $list
        );
        return json($data);
    }

    /**
     * 提现详情
     */
    public function withdraw_detail() {
        $mid = $this->getLoginId();
        if (empty($mid)) {
            return $this->returnResult(- 7, '请登录!');
        }
        $id = $this->request->param('id', '0', 'intval');
        if (empty($id)) {
            return $this->returnResult(- 1, '参数错误');
        }
        $item = $this->rechargeModel->getById($mid, $id);
        if (empty($item)) {
            return $this->returnResult(- 1, '参数错误');
        }

        $member = MemberModel::selectOneMemberByID($mid);
        $bank = Payment::selectOnePaymentByID($item['bank_id']);
        $data = [
            'id' => $item['id'],
            'nickname' => $member['name'],
            'avatar' => $member['face'],
            'money' => $item['money'],
            'type_name' => '提现',
            'reviewer' => '总部',
            'status' => $item['ifpass'],
            'status_name' => $this->_getStatusName($item['ifpass']),
            'ctime' => date('Y/m/d', strtotime($item['addtime'])),
            'bank' => [
                'paytype_name' => $this->_getPaymentTypeName($bank['paytype']),
                'name' => $bank['name'],
                'addr' => $bank['addr'],
                'num' => $bank['num']
            ]
        ];
        return $this->returnResult(1, '', $data);
    }

    public function records() {
        $mid = $this->getLoginId();
        if (empty($mid)) {
            return $this->returnResult(- 7, '请登录!');
        }
        $startDate = $this->request->param('start_date');
        $endDate = $this->request->param('end_date');
        $type = $this->request->param('type', 1);
        $page = $this->request->param('page', 1);
        $size = 10;

        $where = [];
        $where['mid'] = $mid;
        if ($startDate) {
            $where['addtime'] = [
                '>',
                date('Y-m-d', strtotime($startDate))
            ];
        }
        if ($endDate) {
            $where['addtime'] = [
                '<',
                date('Y-m-d 23:59:59', strtotime($endDate))
            ];
        }
        if ($startDate && $endDate) {
            $where['addtime'] = [
                'between',
                [
                    date('Y-m-d', strtotime($startDate)),
                    date('Y-m-d 23:59:59', strtotime($endDate))
                ]
            ];
        }

        // 充值
        $rechargeWhere = array_merge($where, [
            'type' => 1,
            'ifpass' => 1
        ]);
        $rechargePrice = floatval(Recharge::instance()->where($rechargeWhere)->sum('money'));

        // 扣费
        $orderWhere = array_merge($where, [
            'ispay' => [
                'in',
                OrderUtil::getPayOrderStatus()
            ]
        ]);
        $orderPrice = floatval(Goodsorder::instance()->where($orderWhere)->sum('total'));

        // 提现
        $withdrawWhere = array_merge($where, [
            'type' => 2,
            'ifpass' => 1
        ]);
        $withdrawPrice = floatval(Recharge::instance()->where($withdrawWhere)->sum('money'));

        $income = $rechargePrice;
        $outcome = $orderPrice + $withdrawPrice;
        $list = [];
        $count = 0;
        if ($type == 1) {
            $items = Recharge::instance()->where($rechargeWhere)
                ->page($page, $size)
                ->order('id', 'desc')
                ->select();
            $count = Recharge::instance()->where($rechargeWhere)->count();

            $mids = array_map(function ($item) {
                return $item['mid'];
            }, $items);
            if ($mids) {
                $memMap = MemberModel::instance()->getItemsByIds($mids, 'name,face');
            }
            foreach ($items as $item) {
                $mid = $item['mid'];
                $tmp = [
                    'id' => $item['id'],
                    'nickname' => $memMap[$mid]['name'],
                    'avatar' => $memMap[$mid]['face'],
                    'money' => $item['money'],
                    'type_name' => '提现',
                    'ctime' => date('Y/m/d', strtotime($item['addtime']))
                ];
                $list[] = $tmp;
            }
        } elseif ($type == 2) {
            $items = Goodsorder::instance()->where($orderWhere)
                ->order('id', 'desc')
                ->select();
            $count = Goodsorder::instance()->where($orderWhere)->count();
            $mids = array_map(function ($item) {
                return $item['mid'];
            }, $items);
            if ($mids) {
                $memMap = MemberModel::instance()->getItemsByIds($mids, 'name,face');
            }
            foreach ($items as $item) {
                $tmp = [
                    'id' => $item['id'],
                    'nickname' => $memMap[$mid]['name'],
                    'avatar' => $memMap[$mid]['face'],
                    'money' => $item['total'],
                    'type_name' => '',
                    'ctime' => date('Y/m/d', strtotime($item['addtime']))
                ];
                $list[] = $tmp;
            }
        } elseif ($type == 3) {
            $items = Recharge::instance()->where($withdrawWhere)
                ->order('id', 'desc')
                ->select();
            $count = Recharge::instance()->where($withdrawWhere)->count();
            $mids = array_map(function ($item) {
                return $item['mid'];
            }, $items);
            if ($mids) {
                $memMap = MemberModel::instance()->getItemsByIds($mids, 'name,face');
            }
            foreach ($items as $item) {
                $mid = $item['mid'];
                $tmp = [
                    'id' => $item['id'],
                    'nickname' => $memMap[$mid]['name'],
                    'avatar' => $memMap[$mid]['face'],
                    'money' => $item['money'],
                    'type_name' => '充值',
                    'ctime' => date('Y/m/d', strtotime($item['addtime']))
                ];
                $list[] = $tmp;
            }
        }
        $data = [
            'income' => $income,
            'outcome' => $outcome,
            'list' => $list,
            'total_page' => (int) ceil($count * 1.0 / $size)
        ];

        return $this->returnResult(1, '', $data);
    }

    public function outcome() {
        $mid = $this->getLoginId();
        if (empty($mid)) {
            return $this->returnResult(- 7, '请登录!');
        }
    }

    /**
     * 订单奖励
     */
    public function order_rewards() {
        $date = $this->request->param('date', date('Y-m-d', time()));

        $date = date('Ymd', strtotime($date));
        $mid = $this->getLoginId();
        // 选中当月时间
        $monthWhere = [
            'mid' => $mid,
            'type' => 1,
            'date' => [
                'between',
                [
                    date('Ym01', strtotime($date)),
                    date('Ymd', strtotime(date('Y-m-01', strtotime($date)) . '+1 month -1 day'))
                ]
            ]
        ];
        $dayWhere = [
            'mid' => $mid,
            'type' => 1,
            'date' => $date
        ];
        $totalWhere = [
            'mid' => $mid,
            'type' => 1
        ];
        $total = RewardModel::instance()->where($totalWhere)->sum('money');
        $month = RewardModel::instance()->where($monthWhere)->sum('money');
        $items = RewardModel::instance()->where($dayWhere)
            ->order('id', 'desc')
            ->select();

        $list = [];
        $subIds = ArrayUtil::getKeyValuesFromArray($items, 'sub_mid');
        $midsMap = [];
        if ($subIds) {
            $midsMap = MemberModel::instance()->getItemsByIds($subIds, 'grade');
        }
        foreach ($items as $item) {
            $money = $item['status'] == 1 ? $item['money'] : $item['num'] * $item['value']; // 根据个数计算金额
            $list[] = [
                'payer' => '总部',
                'money' => $money,
                'recom_name' => $item['display_name'],
                'phone' => $item['rem_tel'],
                'lever' => WebUtil::getGradeName($midsMap[$item['sub_mid']]),
                'status' => $item['status'] == 1 ? '已结算' : '未结算',
                'date' => $item['status'] == 0 ? date('Y-m-d',strtotime($item['date'])) : date('Y-m-d', $item['settlement_time'])
            ];
        }
        $data = [
            'month' => $month, // 月奖励
            'total' => $total, // 总奖励
            'list' => $list
        ];
        return $this->returnResult(1, '', $data);
    }

    /**
     * 团队奖励
     */
    public function team_rewards() {
        $date = $this->request->param('date', date('Y年 m月', time()));
        $date = $date . " 01日";
        $date = str_replace(' ', '-', $date);
        $date = preg_replace("/[年|月|日]/", '', $date);

        $mid = $this->getLoginId();

        $month = 0; // 月结
        $total = 0; // 总数
        $montyDate = date('Ym', strtotime($date));

        // 总的结算奖励
        $totalWhere = [
            'mid' => $mid,
            'type' => 2,
            'status' => 1
        ];
        $total = RewardModel::instance()->where($totalWhere)->sum('money');
        $total = floatval($total);

        // 总的未结算奖励
        $totalUnSettleWhere = [
            'mid' => $mid,
            'type' => 2,
            'status' => 0,
            'month' => $montyDate
        ];
        $totalUnSettleCount = RewardModel::instance()->where($totalUnSettleWhere)->sum('num');

        // 总的子级未结算奖励
        // 一级子集
        $subMids = \app\common\model\MemberModel::instance()->getFristMebmerIdsByPid($mid);
        $subListMap = [];
        if ($subMids) {
            $subTotalUnSettleWhere = [
                'mid' => [
                    'in',
                    $subMids
                ],
                'type' => 2,
                'status' => 0,
                'month'=> $montyDate
            ];
            $subListMap = RewardModel::instance()->where($subTotalUnSettleWhere)->column('id,num'); // 查询子级每个人金额
        }

        // 选中的月份的金额
        $monthWhere = [
            'mid' => $mid,
            'type' => 2,
            'month' => $montyDate
        ];
        $items = RewardModel::instance()->where($monthWhere)->select();

        $list = [];

        $price = 0;
        $rebateValue = RebateModel::instance()->where('type', 'TEAM')->value('value');
        $values = []; // 价格map
        if ($rebateValue) {
            $values = json_decode($rebateValue, true);
            krsort($values);
        }
        $price = WebUtil::getRewardPrice($values, $totalUnSettleCount);
        $subUnsettlePrice = 0;
        // 每个子级结算金额
        foreach ($subListMap as $id => $val) {
            $subUnsettlePrice += $val * WebUtil::getRewardPrice($values, $val);
        }
        $unsettlePrice = $totalUnSettleCount * $price - $subUnsettlePrice; // //父级没有结算金额 - 子集没有结算金额
        $total += $unsettlePrice;

        foreach ($items as $item) {

            $money = $item['status'] == 1 ? $item['money'] : $unsettlePrice; // 每个月结算一次,没有结算时,直接赋值的没有借宿那的所有金额
            $list[] = [
                'payer' => '总部',
                'money' => $money,
                'type' => $item['display_name'],
                'count' => $item['num'],
                'month' => date('Y-m', strtotime($item['date'])),
                'time' => $item['status'] == 1 ? date('Y-m-d H:i', $item['settlement_time']) : '-',
                'status' => $item['status'] == 1 ? '已结算' : '未结算'
            ];
            $month += $money;
        }

        $data = [
            'month' => $month, // 月奖励
            'total' => $total, // 总奖励
            'list' => $list
        ];
        return $this->returnResult(1, '', $data);
    }

    /**
     * 领导奖励
     */
    public function leader_rewards() {
        $date = $this->request->param('date', date('Y年 m月', time()));
        $date = $date . " 01日";
        $date = str_replace(' ', '-', $date);
        $date = preg_replace("/[年|月|日]/", '', $date);

        $mid = $this->getLoginId();

        $month = 0; // 月结
        $total = 0; // 总数

        $type = 3;
        $montyDate = date('Ym', strtotime($date));
        // 总的结算奖励
        $totalWhere = [
            'mid' => $mid,
            'type' => $type,
            'status' => 1
        ];
        $total = RewardModel::instance()->where($totalWhere)->sum('money');

        $total = floatval($total);

        $rebate = RebateModel::instance()->where('type', 'LEADER')->find();
        $price = 0;
        $rebate && $price = floatval($rebate['value']);

        // 总的未结算奖励
        $totalUnSettleWhere = [
            'mid' => $mid,
            'type' => $type,
            'status' => 0,
            'month' => $montyDate
        ];
        $totalUnSettleCount = RewardModel::instance()->where($totalUnSettleWhere)->sum('num');

        // 总的子级未结算奖励
        // 一级子集
        $subMids = \app\common\model\MemberModel::instance()->where('fid', $mid)
            ->where('grade', 2)
            ->column('id');
        $subUnSettleCount = 0;
        if ($subMids) {
            $subTotalUnSettleWhere = [
                'mid' => [
                    'in',
                    $subMids
                ],
                'type' => $type,
                'status' => 0,
                'month' => $montyDate
            ];
            $subUnSettleCount = RewardModel::instance()->where($subTotalUnSettleWhere)->sum('num'); // 查询子级每个人金额
        }
        // 每个子级结算金额
        $unsettlePrice = ($totalUnSettleCount - $subUnSettleCount) * $price; // //父级没有结算金额 - 子集没有结算金额

        $total += $unsettlePrice;

        // 选中的月份的金额
        $monthWhere = [
            'mid' => $mid,
            'type' => $type,
            'month' => $montyDate
        ];
        $items = RewardModel::instance()->where($monthWhere)->select();

        Log::write("log:$total,$totalUnSettleCount,$subUnSettleCount,$price,$unsettlePrice");
        $list = [];

        foreach ($items as $item) {
            $money = $item['status'] == 1 ? $item['money'] : $unsettlePrice; // 根据个数计算金额
            $list[] = [
                'payer' => '总部',
                'money' => $money,
                'type' => $item['display_name'],
                'count' => $item['num'],
                'month' => date('Y-m', strtotime($item['date'])),
                'time' => $item['status'] == 1 ? date('Y-m-d H:i', $item['settlement_time']) : '-',
                'status' => $item['status'] == 1 ? '已结算' : '未结算'
            ];
            $month += $money;
        }

        $data = [
            'month' => $month, // 月奖励
            'total' => $total, // 总奖励
            'list' => $list
        ];
        return $this->returnResult(1, '', $data);
    }

    private function _getStatusName($status) {
        return WebUtil::getStatusName($status);
    }

    private function _getPaymentTypeName($paytype) {
        return WebUtil::getPaymentTypeName($paytype);
    }

    /**
     * 前台修改密码
     */
    public function xgmm() {
        $mid = $this->getLoginId();
        $all = Request::instance()->param();
        $all['mid'] = $mid;
        if (empty($all['password'])) {
            return $this->returnResult(- 1, '密码不能为空');
        }
        $all['password'] = md5(trim($all['password']));
        $data = MemberModel::xgmm($all);
        if ($data) {
            return $this->returnResult(1, '修改成功');
        } else {
            return $this->returnResult(- 1, '修改失败');
        }
    }

    /**
     * 代理邀请*
     */
    public function yaoqing() {
        $mid = $this->getLoginId();

        $Response = new Response();
        $Response->header('Content-Type', 'image/png');
        $Response->contentType('Content-Type: image/png');

        $Response->content(qrcode(input('server.REQUEST_SCHEME') . '://' . input('server.SERVER_NAME') . '/mobile/index/register.html?fid=' . $mid, 8));

        return $Response;
    }
}